import { findHeadCategory } from '@/api/home.js'
import { category } from '@/utils/constant.js'
export default {
  namespaced: true,
  state: {
    list: category.map((item, index) => ({ id: index, name: item }))
  },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // 控制一二级菜单的显示隐藏
    hide (state, id) {
      const cate = state.list.find(item => item.id === id)
      if (cate) {
        cate.open = false
      }
    },
    // 控制一二级菜单的显示隐藏
    show (state, id) {
      const cate = state.list.find(item => item.id === id)
      if (cate) {
        cate.open = true
      }
    }
  },
  actions: {
    async getList (ctx) {
      try {
        const ret = await findHeadCategory()
        // 给每一项数据添加一个占位符
        ret.result.forEach(item => {
          item.open = false
        })
        ctx.commit('setList', ret.result)
      } catch {
        alert('获取分类数据失败')
      }
    }
  }
}
